D:\a\scloud-dns\scloud-dns\src\workers\manager\mod.rs
Line | Count | Source |
1 | | pub(crate) mod channels_generation; |
2 | | |
3 | | use once_cell::sync::Lazy; |
4 | | use std::sync::Mutex; |
5 | | use tokio::sync::{Mutex as TMutex, Notify}; |
6 | | |
7 | 1 | static SCLOUD_WORKER_ID_LIST: Lazy<Mutex<Vec<u64>>> = Lazy::new(|| Mutex::new(Vec::new())); |
8 | | |
9 | 69 | pub(crate) fn generate_worker_id() -> u64 { |
10 | 69 | let mut list = SCLOUD_WORKER_ID_LIST.lock().unwrap(); |
11 | 69 | let result = list.len() as u64 + 1; |
12 | 69 | list.push(result); |
13 | 69 | result |
14 | 69 | } |
15 | | |
16 | | pub(crate) struct StartGate { |
17 | | next_id: TMutex<u64>, |
18 | | notify: Notify, |
19 | | } |
20 | | |
21 | | impl StartGate { |
22 | 18 | pub(crate) fn new(first_id: u64) -> Self { |
23 | 18 | Self { |
24 | 18 | next_id: TMutex::new(first_id), |
25 | 18 | notify: Notify::new(), |
26 | 18 | } |
27 | 18 | } |
28 | | |
29 | 0 | pub(crate) async fn wait_turn(&self, my_id: u64) { |
30 | | loop { |
31 | | { |
32 | 0 | let next = *self.next_id.lock().await; |
33 | 0 | if next == my_id { |
34 | 0 | return; |
35 | 0 | } |
36 | | } |
37 | 0 | self.notify.notified().await; |
38 | | } |
39 | 0 | } |
40 | | |
41 | 18 | pub(crate) async fn done(&self) { |
42 | 18 | let mut next = self.next_id.lock().await; |
43 | 18 | *next += 1; |
44 | 18 | drop(next); |
45 | 18 | self.notify.notify_waiters(); |
46 | 18 | } |
47 | | } |